package com.dailyblue.javaee.filter;

import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpFilter;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
//会影响运行所以加注释
//@WebFilter("/*")
public class BlacklistFilter extends HttpFilter {

    @Override
    protected void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
        String ip = request.getRemoteAddr();
        // 192.168.9.19
        String s = ip.substring(ip.length() - 1);
        int i = Integer.parseInt(s);
        if (i % 2 == 0) {
            chain.doFilter(request, response);
        }
        PrintWriter out = response.getWriter();
        out.println("<h2 style='color:red'>你的IP不能访问该网站</h2>");

    }
}